﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Text.RegularExpressions;

namespace StoragePeriod.COMP
{
    /// <summary>
    /// 配置信息
    /// </summary>
    public class Config
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public static string DBConnectionString
        {
            get { return Crypto.PriDecrypt(ConfigurationManager.ConnectionStrings["ConnString"].ToString()); }
        }
        /// <summary>
        /// 设置用户有效期检测是否打开
        /// </summary>
        public static bool UserEffective
        {
            get { return GetAppSettings("UserEffective") == "true"; }
        }

        /// <summary>
        /// 分割符（用于数据库StrSplit函数）
        /// </summary>
        public static string Separation
        {
            get { return "々"; }
        }
        /// <summary>
        /// 无数据时显示
        /// </summary>
        public static string NoDataTips { get { return "对不起，暂无数据！"; } }
        /// <summary>
        /// 无数据时显示 Tr
        /// </summary>
        /// <param name="rowNum">需要合并的单元格数量</param>
        public static string NoDataTr(int rowNum) { return string.Format("<tr><td colspan='{0}'>{1}</td></tr>", rowNum, NoDataTips); }
        /// <summary>
        /// 当itemCount等于0时，显示 Tr
        /// </summary>
        /// <param name="itemCount"></param>
        /// <param name="rowNum"></param>
        /// <returns></returns>
        public static string NoDataTr(int itemCount, int rowNum) { return itemCount > 0 ? "" : string.Format("<tr><td colspan='{0}'>{1}</td></tr>", rowNum, NoDataTips); }

        

        


        #region 数据接口配置
       
        /// <summary>
        /// 数据库服务器地址
        /// </summary>
        public static string DBServer { get { return GetDBConnctionString("server"); } }
        /// <summary>
        /// 数据库连接用户名
        /// </summary>
        public static string DBUid { get { return GetDBConnctionString("uid"); } }
        /// <summary>
        /// 数据库连接密码
        /// </summary>
        public static string DBPwd { get { return GetDBConnctionString("pwd"); } }
        /// <summary>
        /// 数据库名称
        /// </summary>
        public static string DBDataBase { get { return GetDBConnctionString("database"); } }
       
        #endregion

        /// <summary>
        /// 获取web.config AppSettings值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppSettings(string key)
        {
            return ConfigurationManager.AppSettings[key].ToString();
        }
        /// <summary>
        /// 获取数据库连接字符串中某一项
        /// 王洪岐 20111014
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetDBConnctionString(string key)
        {
            MatchCollection matches = Regex.Matches(DBConnectionString, @"([A-Za-z]*)=([^;]*)");
            foreach (Match m in matches)
            {
                if (m.Groups[1].Value.Trim().ToLower() == key)
                {
                    return m.Groups[2].Value;
                }
            }
            return string.Empty;
        }

        #region//附件存取地址配置
        /// <summary>
        /// 获取活动公告存取地址
        /// </summary>
        /// <returns></returns>
        public static string GetActAnnAttachSavePath()
        {
            return "Announcement";
        }
        /// <summary>
        /// 获取活动规则存取地址
        /// </summary>
        /// <returns></returns>
        public static string GetActyRulesAttachSavePath()
        {
            return "ActionRule";
        }
        /// <summary>
        /// 获取奖励公告存取地址
        /// </summary>
        /// <returns></returns>
        public static string GetAwardAttachSavePath()
        {
            return "Award";
        }
        /// <summary>
        /// 获取任务附件存取地址
        /// </summary>
        /// <returns></returns>
        public static string GetTaskAttachSavePath()
        {
            return "Task";
        }
        #endregion

    }
}
